package com.gfxy.modules.items.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gfxy.modules.items.entity.Zckj;
import com.gfxy.modules.items.entity.teachingresult.TeachingResultContent;
import com.gfxy.modules.items.mapper.ZckjMapper;
import com.gfxy.modules.items.service.IZckjService;
import com.gfxy.modules.items.vo.*;
import com.gfxy.modules.excel.ExcelUtil;
import org.apache.poi.ss.usermodel.Row;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description: 众创空间
 * @Author: jeecg-boot
 * @Date:   2021-08-29
 * @Version: V1.0
 */
@Service
public class ZckjServiceImpl extends AbstractProjectServiceImpl<ZckjMapper, Zckj> implements IZckjService {

 protected int memberCellNum = 3;
	@Override
	protected void pass(String id, String isPass) {

	}

	@Override
	protected Zckj getOneProject(Row row) {
		String date = ExcelUtil.formatCell(row.getCell(0));
		String projectName  = ExcelUtil.formatCell(row.getCell(1));
		String classess = ExcelUtil.formatCell(row.getCell(2));

		Zckj zxjpk = new Zckj();

		zxjpk.setDate(date);

		zxjpk.setClassess(translateClassess(classess));
		zxjpk.setProjectName(projectName);
		zxjpk.setIsPass(0);
		//这个地方不要忘了-算总分
		zxjpk.setTotalScore(this.getTotalScore(zxjpk));
		return zxjpk;
	}
	@Override
	protected String[] getMemberArray(Row row) {
		String nameStrs = ExcelUtil.formatCell(row.getCell(memberCellNum));
		return nameStrs.split(memberNameSeparator);
	}

//	@Override
//	public List<String> importExcel(MultipartFile file) throws IOException {
//		List<String> result = new ArrayList<>();
//		InputStream inputStream = file.getInputStream();
//		if (!(inputStream).markSupported()) {
//			inputStream = new PushbackInputStream(inputStream, 8);
//		}
//
//		Workbook book = WorkbookFactory.create(inputStream);
//		Sheet sheet = book.getSheetAt(0);
//
//		List<ZckjPage> list = new ArrayList<>();
//
//		for (int i = 1; i <= sheet.getLastRowNum(); i++) {
//			Row row = sheet.getRow(i);
//
//			String year = ExcelUtil.formatCell(row.getCell(0));
//			String project_name = ExcelUtil.formatCell(row.getCell(1));
//			String classess = ExcelUtil.formatCell(row.getCell(2));
//
//			String members = ExcelUtil.formatCell(row.getCell(3));
//			String[] memberArray = members.split("、");
//
//			ZckjPage zckj = new ZckjPage();
//			zckj.setYear(year);
//			zckj.setProjectName(project_name);
//			zckj.setClassess(translateClassess(classess));
//
//
//			List<MemberList> memberListList = new ArrayList<>();
//			for (int j = 0; j < memberArray.length; j++) {
//				String memberStr = memberArray[j];
//				List<MemberList> memberLists = memberListMapper.selectByRealName(memberStr.trim());
//				if(memberLists.size()==1){
//
//					MemberList memberList = memberLists.get(0);
//					memberList.setRanked(j+1);
//					memberList.setProjectType("1");
//					memberList.setIsCustom("0");
//					memberList.setProportion(ExcelUtil.computeProportion(j+1,memberArray.length));
//					memberListList.add(memberList);
//
//				} else if(memberLists.size() == 0){
//
//					result.add(zckj.getProjectName() +  "中，教师:" + memberStr + " 数据不存在，请手动处理");
//					MemberList memberList = new MemberList();
//					memberList.setRanked(j+1);
//					memberList.setProjectType("18");
//					memberList.setIsCustom("0");
//					memberListList.add(memberList);
//				} else if(memberLists.size()>1){
//
//					result.add(zckj.getProjectName() +  "中，教师:" + memberStr + " 存在同名教师，请手动处理");
//					MemberList memberList = new MemberList();
//					memberList.setRanked(j+1);
//					memberList.setProjectType("18");
//					memberList.setIsCustom("0");
//					memberListList.add(memberList);
//				}
//			}
//
//			double totalScore = this.getTotalScore(zckj.getClassess());
//			zckj.setTotalScore(totalScore);
//			ExcelUtil.computeScore(memberListList,totalScore);
//
//			zckj.setMemberListList(memberListList);
//			list.add(zckj);
//		}
//
//		for (ZckjPage page : list) {
//			Zckj po = new Zckj();
//			BeanUtils.copyProperties(page, po);
//			this.saveMain(po, page.getMemberListList());
//		}
//
//		return result;
//	}

	@Override
	public Double getTotalScore(Zckj zckj) {
		Integer classess = zckj.getClassess();
		double result = 0.0;
		if(classess == 1){
			result = 150	;
		} else if(classess == 2 ) {
			result = 75;
		} else if(classess == 3 ) {
			result = 20;
		}
		return result;
	}

	protected double computeProportion(int ranked, int totalNum){

		if(totalNum==1){
			return 1.0;
		} else if(totalNum == 2){
			if(ranked ==1){
				return 0.6;
			} else if(ranked == 2){
				return 0.4;
			} else {
				return 0.0;
			}
		} else if(totalNum == 3){
			if(ranked ==1){
				return 0.5;
			} else if(ranked == 2){
				return 0.3;
			} else if(ranked == 3){
				return 0.2;
			} else {
				return 0.0;
			}
		} else if(totalNum == 4){
			if(ranked ==1){
				return 0.5;
			} else if(ranked == 2){
				return 0.3;
			} else if(ranked == 3){
				return 0.1;
			} else if(ranked == 4){
				return 0.1;
			} else {
				return 0.0;
			}
		} else if(totalNum == 5){
			if(ranked ==1){
				return 0.45;
			} else if(ranked == 2){
				return 0.25;
			} else if(ranked == 3){
				return 0.1;
			} else if(ranked == 4){
				return 0.1;
			} else if(ranked == 5){
				return 0.1;
			} else {
				return 0.0;
			}
		}
		 else if(totalNum >=6){
			if(ranked == 1){
				return 0.5;
			} else if(ranked == 2){
				return 0.2;
			} else if (ranked>=3){
				return Math.floor(0.5*10000/(totalNum-2))/10000.0;
			}
		} else {
			return 0.0;
		}
		 return 0.0;
	}
//	@Override
//	public Double[] getMemberListScore(double totalScore, int memberNum) {
//		return new Double[0];
//	}


	private int translateClassess(String name){
		if(name == null || name.equals("")){
			return 0;
		} else if(name.equals("国家级")){
			return 1;
		} else if(name.equals("省级")){
			return 2;
		} else if(name.equals("校级")){
			return 3;
		} else {
			return 0;
		}
	}



	@Override
	public IPage<ZckjMemberPage> getZckjMemberPageListByYear(String date, Integer pageNo, Integer pageSize) {
		Page<ZckjMemberPage> zckjMemberPageIPage = new Page<>(pageNo, pageSize);
		return zckjMemberPageIPage.setRecords(this.baseMapper.selectZckjMemberPageListByYear(date));

	}

	@Override
	public List<ZckjMemberPage> getZckjMemberPageListByYear(String date) {
		return this.baseMapper.selectZckjMemberPageListByYear(date);
	}
//
//
//	@Override
//	public double getTotalScore(Integer classess){
//		double result = 0.0;
//		if(classess == 1){
//			result = 150	;
//		} else if(classess == 2 ) {
//			result = 75;
//		} else if(classess == 3 ) {
//			result = 20;
//		}
//		return result;
//	}

	@Override
	protected List<TeachingResultContent> createTeachingResultContent(Zckj jxcg)  {

		List<TeachingResultContent> result = new ArrayList<>();

		// 类型
		TeachingResultContent classTeachingResultContent = new TeachingResultContent();
		classTeachingResultContent.setType(1);
		classTeachingResultContent.setLevel(jxcg.getClassess());

		try {
			classTeachingResultContent.setValue(dictTranslater.getDictValue(ZckjMemberPage.class, "classess", String.valueOf(jxcg.getClassess())));
			result.add(classTeachingResultContent);
		} catch (NoSuchFieldException e) {
			log.error(e.getMessage(),e);
		}

		return result;
	}

}
